<template>
  <el-row style="width: 100%" class="treetools">
    <el-col :span="12">
      <i class="el-icon-upload"></i>
      {{ data.name }}
    </el-col>
    <el-col :span="12" style="text-align: right">
      <span class="manager">{{ data.manager }}</span>
      <el-dropdown @command="commandHandler">
        <span class="el-dropdown-link"> 操作<i class="el-icon-arrow-down el-icon--right"></i> </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item command="check" v-if="isheader === false">查看部门</el-dropdown-item>
          <el-dropdown-item command="del" v-if="isheader === false">删除部门</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-col>
  </el-row>
</template>

<script>
import { Message } from 'element-ui'
import { delDeptApi } from '@/api/departments'
export default {
  // data:用来接收当前行的数据
  props: ['data', 'isheader'],
  methods: {
    commandHandler(command) {
      if (command === 'add') {
        // console.log('添加子部门')
        this.$emit('addShow', this.data.id)
      } else if (command === 'check') {
        // console.log('查看子部门')
        this.$emit('editShow', this.data.id)
      } else {
        this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            await delDeptApi(this.data.id)
            // 子组件调用父组件 this.$emit
            // 更新数据
            this.$emit('getNewData')
          })
          .catch(() => {
            // console.log('取消')\
            Message('已取消删除')
          })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.treetools {
  .manager {
    margin-right: 10px;
  }
}
</style>
